<?php
include_once('admin.inc.php');

$do = get_param("do");
switch($do){
	case 'indexList':
		$model = new Model("index_list");
		$list = $model->order('`order`')->get();
		$smarty->assign("list", $list);
		$smarty->display('admin/indexList.html'); 
	break;
	case 'addIndexList':
		$proModel = new Model("product");
		if( get_param('submit') ){
			$model = new Model("index_list");
			$data = get_data();
			if( empty($data['link']) || empty($data['content']) || empty($data['title']) ){
				msg('必填项不能为空');
			}
			$data['order'] = empty($data['order']) ? 1 : $data['order'];
			$rs = $model->value($data)->add();
			if( $rs>0 ){
				msg('添加成功','index.php?do=indexList');
			}else{
				msg('添加失败',-1);
			}			
			exit;
		}
		$product = $proModel->order('id DESC')->get();
		$smarty->assign("product", $product);
		$smarty->assign("pageTitle","添加项目");
		$smarty->display('admin/addIndexList.html');
	break;
	case 'editIndexList':
		$model = new Model("index_list");
		$proModel = new Model("product");
		$id = intval(get_param('id'));
		if( empty($id) ) msg('非法ID',-1);
		if( get_param('submit') ){
			$data = get_data();
			if( empty($data['link']) || empty($data['content']) || empty($data['title']) ){
				msg('必填项不能为空');
			}
			$data['order'] = empty($data['order']) ? 1 : $data['order'];
			$rs = $model->value($data)->update();
			if( $rs ){
				msg('修改成功','index.php?do=indexList');
			}else{
				msg('修改失败',-1);
			}			
			exit;
		}
		$listinfo = $model->where("id=$id")->get_one();
		$smarty->assign("listinfo", $listinfo);
		$product = $proModel->order('id DESC')->get();
		$smarty->assign("product", $product);
		$smarty->assign("pageTitle","修改项目");
		$smarty->assign("do", "editIndexList");
		$smarty->display('admin/addIndexList.html');
	break;
	case 'delIndexList':
		$model = new Model("index_list");
		$id = intval(get_param('id'));
		if( empty($id) ) msg('非法ID',-1);
		if( $model->where("id=$id")->del() ){
			ajax_return(array('status'=>'success', 'info'=>'删除成功')); 
		}
	break;
	case 'index':
	default:
		$smarty->display('admin/index.html');
	break;
}
